// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {



    @property(cc.Node)
    bg: cc.Node = null;

    @property
    scaleType: number = 1;

    width: number = 750;
    height: number = 1334;
    // LIFE-CYCLE CALLBACKS:

    onLoad() {

        switch (this.scaleType) {
            case 1:
                this.node.scale *= this.getUIScale();
                break;
            case 2:
                this.node.scale *= this.getUIScaleX();
                break;
            case 3:
                this.node.scale *= this.getUIScaleY();
                break;
        }

        this.scaleBg()

    }

    start() {

    }

    /**
     * 适配大小
     * @returns {number}
     */
    getUIScale() {
        const width = cc.winSize.width;
        const height = cc.winSize.height;
        let scale = 1;
        if (width / this.width < height / this.height) {
            scale = width / this.width;
        } else {
            scale = height / this.height;
        }
        return scale;
    }

    /**
     * 适配大小 X
     * @returns {number}
     */
    getUIScaleX() {
        const width = cc.winSize.width;
        return width / this.width;
    }

    /**
     * 适配大小 Y
     * @returns {number}
     */
    getUIScaleY() {
        const height = cc.winSize.height;
        return height / this.height;
    }


    scaleBg() {
        const scale = this.getUIScale();

        if (!this.bg) {
            return;
        }


        switch (this.scaleType) {
            case 1:
                this.bg.scale *= this.getUIScale();
                break;
            case 2:
                this.bg.scale *= this.getUIScaleX();
                break;
            case 3:
                this.bg.scale *= this.getUIScaleY();
                break;
        }


    }

    // update (dt) {}
}
